<?php

/*
************商品分类模块***********
*/

class GoodscatAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(4,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function index(){
		$cat=$this->get_cat();
		$Model=M('Goods_model');
		for($i=0;$i<count($cat);$i++){
			$model=$Model->where('model_id='.$cat[$i]['model'])->find();
			$cat[$i]['model_n']=$model['model_name'];
		}
		$this->assign("cat",$cat);
		$this->display();
	}

	//1、添加分类
	public function add(){
		$this->pid=$this->_get('pid');
		$this->mid=$this->_get('model');
		//获取商品模型
		$this->model=R('Goodsmodel/get_model');
		//获取商品分类
		$this->cat_list=$this->get_cat();
		$this->display();
	}
    //1-2、保存（添加）分类
	public function add_cat(){
		$Cat=M('Goods_cat');
		$model_id=$this->_post('model');
		$up_cat=$this->_post('parent');
		$cat_name=$this->_post('name');
		$cat_seo=$this->_post('seo');
		$father=$Cat->where('cat_id='.$up_cat)->find();

		//分类不存在
		$data['model']=$model_id;
		$data['pid']=$up_cat;
		$data['cat_name']=$cat_name;
		$data['seo_key']=$cat_seo;
		$data['is_active'] = $this->_post("is_show");
		$data['is_hot'] = $this->_post("is_hot");
		$data['icon'] = $this->_post("icon");
		$rel=$Cat->add($data);
		$now=$Cat->where('cat_id='.$rel)->find();
		$data2['path']=$father['path'].','.$now['cat_id'];
		$rel2=$Cat->where('cat_id='.$rel)->save($data2);
		if($rel2){
			echo "1";
		}else{
			echo "0";
		}
			
	}

	//2、分类修改
	public function edit(){
		$Cat=M('Goods_cat');
		$Model=M('Goods_model');
		$this->model=R('Goodsmodel/get_model');
		$this->cat_list=$this->get_cat();
		$cat_id=$this->_get('cat');

		$cat=$Cat->join('e_goods_model ON e_goods_cat.model=e_goods_model.model_id')->where('cat_id='.$cat_id)->find();
		$this->parent=$Cat->where('cat_id='.$cat['pid'])->find();
		$this->assign("cat",$cat);
		$this->display();
	}
	//2-2、保存（修改）分类
	public function save_edit(){
		$Cat=M('Goods_cat');
		$map['cat_id']=$this->_post('cid');
		$data['pid']=$this->_post('parent');
		$father=$Cat->where('cat_id='.$data['pid'])->find();
		$data['model']=$this->_post('model');
		$data['cat_name']=$this->_post('name');
		$data['is_hot'] = $this->_post("is_hot");
		$data['seo_key']=$this->_post('seo');
		$data['is_active'] = $this->_post("is_show");
		$data['icon'] = $this->_post("icon");
		$data['path']=$father['path'].",".$map['cat_id'];
		$result=$Cat->where($map)->save($data);

		if($result){
		   echo "1";
		}else{
		   echo "0";
		}
	}

	//更改排序
	public function sorting(){
		$cat=$this->_post('cat');
		$sort=$this->_post('sort');
		$Cat=M('Goods_cat');
		for($i=0;$i<count($cat);$i++){
			$data['sorting']=$sort[$i];
			$Cat->where('cat_id='.$cat[$i])->save($data);
		}
	}

	//删除分类
	/*
	**返回值
	1(成功),0(失败)
	*/
	public function del_cat(){
		$Cat=M('Goods_cat');
		$Goods=M('Goods');
		$id=$this->_post('id');
		$map['cat_id']=$id;
		$map2['path']=array('like','%,'.$id.'%');
		$ck_goods=$Goods->where($map)->select();
		$ck_cat=$Cat->where($map2)->select();
		$num=count($ck_cat);
		if($ck_goods||($num>1)){
			echo '0';
		}else{
			$Cat->where($map)->delete();
			echo '1';
		}
	}

	//序列化分类列表
	public function get_cat(){
		//获取商品分类列表
		import('ORG.Util.Category');
		$cat = new Category('Goods_cat', array('cat_id', 'pid', 'cat_name', 'fullname'));
		return $cat->getList('',0,'sorting');
		//echo "<pre>";
		//print_r($cat->getList());
	}

  
}
?>